package com.shu.qq.viewmodels

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.shu.qq.data.QQService
import com.shu.qq.data.bean.BaseRequest
import com.shu.qq.data.bean.LoginResponse
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import kotlinx.coroutines.yield
import javax.inject.Inject

const val TAG = "shu-check"
@HiltViewModel
class LoginViewModel @Inject constructor(

) : ViewModel(){
    @Inject
    lateinit var qqService: QQService

    var account = MutableLiveData<String>("1391524307")
    var password = MutableLiveData<String>("12345678")
    var loginAble = MutableLiveData(false)
    var isLogging = MutableLiveData(false) //用户登录进度条显示
//    var loginResult = MutableLiveData<BaseResponse<Void>>()


    fun login(){
        isLogging.value = true
        viewModelScope.launch() {
            Log.d(TAG, "login: 检查协程")
            val loginResult : LoginResponse
            account.value?.let { accountValue ->
                password.value?.let { passwordValue ->
                    Log.d(TAG, "login: 执行请求")
                    val requestBody = BaseRequest.Builder()
                        .put("account", accountValue)
                        .put("password", passwordValue)
                        .build()

                    try {
                        loginResult =  qqService.login(requestBody)
                    } catch (e:  Exception) {
                        Log.d(TAG, "login: 异常 ${e.message}")
                    }

                }
            }

        }
        isLogging.value = false
    }
}